package com.itheima.reggie.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public Page findByPage(Integer pageNum, Integer pageSize, String name) {
        //1. 分页查询菜品信息
        //1-1 设置分页条件
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //1-2 设置业务条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        //1-3 执行查询
       page = dishMapper.selectPage(page, queryWrapper);


//2. 遍历分页列表,得到每一个菜品
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
                //查询口味
                LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
                dish.setFlavors(dishFlavors);
            }

        }


        return page;
    }
//菜品新增
    @Override
    public void save(Dish dish) {
        //保存菜品
        dishMapper.insert(dish);

        if (CollectionUtil.isNotEmpty(dish.getFlavors())) {
            for (DishFlavor dishFlavor : dish.getFlavors()) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }
//菜品修改回显
    @Override
    public Dish updateSelect(Long id) {
        Dish dish = dishMapper.selectById(id);

        //回显菜品
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //回显口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavors);

        return dish;
    }
//修改菜品
    @Override
    public void update(Dish dish) {
        dishMapper.updateById(dish);
        //删除之前的口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);
        //保存新的口味
        if (CollectionUtil.isNotEmpty(dish.getFlavors())) {
            for (DishFlavor flavor : dish.getFlavors()) {
                flavor.setDishId(dish.getId());


            }
        }

    }

    //批量删除
    @Override
    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<Dish>();
        queryWrapper.eq(Dish::getStatus, 0);
        queryWrapper.in(Dish::getId, ids);
        Integer count = dishMapper.selectCount(queryWrapper);
        if (count>0) {
            throw new CustomException("有些餐品未停售,禁止删除");
        }
        //删除口味
        if (CollectionUtil.isNotEmpty(ids)) {
            for (Long id : ids) {
                LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(DishFlavor::getDishId, id);
                dishFlavorMapper.delete(wrapper);
                dishMapper.deleteById(id);
            }
        }
    }
//批量停售起售
    @Override
    public void status(Integer sratus, List<Long> ids) {

        //设置where部分
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);
        Dish dish = new Dish();
        dish.setStatus(sratus);
        //更新
        dishMapper.update(dish, queryWrapper);
    }
//回显菜品
    @Override
    public List<Dish> finsAll(Long categoryId,String name) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getStatus, 1)//在售
                .eq(categoryId!=null,Dish::getCategoryId, categoryId)
        .like(StringUtils.isNotEmpty(name),Dish::getName,name);//分类id
        // 执行查询
        return dishMapper.selectList(queryWrapper);

    }
//菜品列表
    @Override
    public List<Dish> findList(Long categoryId, String name) {
//dish查询菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getStatus, 1)
                .in(categoryId != null, Dish::getCategoryId, categoryId)
                .like(StringUtils.isNotEmpty(name), Dish::getName, name);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        //dish查询口味
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
                dishFlavorWrapper.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(dishFlavorWrapper);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }


}
